import g
import config
import asyncio
import server.gateway.main as gateway
import server.worker.main as worker
import time

"""通过配置一键启动所有服务"""


class GameManage(object):
    def __init__(self, *args, **kwargs):
        pass

    def run(self):
        self.start_worker()

        # 等待会让worker完全启动
        time.sleep(2)

        self.start_gateway()
        self.start_timer()
        self.start_sign()

    # 启动网关
    @staticmethod
    def start_gateway():
        if g.get_game_ver() != "cross":
            # 遍历配置，启动本机上的网关服务
            for gatewayUrl in config.GatewayUrlList:
                ip, port = gatewayUrl
                if ip != config.LocalIp:
                    continue
                gateway.process_run(ip, port)
        print("\n")

    # 启动worker
    @staticmethod
    def start_worker():
        # 遍历配置，启动本机上的网关服务
        for workerUrl in config.WorkerUrlList:
            ip, port = workerUrl
            if ip != config.LocalIp:
                continue

            worker.process_run(ip, port)
        print("\n")

    # 启动定时器
    def start_timer(self):
        pass

    # 启动统计服务
    def start_sign(self):
        pass


async def start():
    # 游戏进程启动
    GameManage().run()


if __name__ == '__main__':
    try:
        asyncio.run(start())
    except KeyboardInterrupt:
        pass
